//
//  JXCalendarViewController.m
//  TestCollectionView
//
//  Created by lemon on 2019/4/22.
//  Copyright © 2019 sunshinfight@163.com. All rights reserved.
//

#import "JXCalendarViewController.h"

@interface JXCalendarViewController ()
@property (nonatomic) JXCalendarView *calendarView;
@property (nonatomic) JXSelectedDateEditView *editView;
@end

@implementation JXCalendarViewController

/**
 带有导航的的日历

 @return 导航控制器
 */
+ (UINavigationController*)navigationControllerWithJXCalendarViewController
{
    id vc = [self new];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    return nav;
}

// MARK: - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
}

- (void)configNavigationBar
{
    self.navigationController.navigationBar.backgroundColor = UIColor.whiteColor;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = UIColor.whiteColor;
    
    UIButton *closeButton = [UIButton new];
    [closeButton setImage:[UIImage imageNamed:@"nav_close"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    closeButton.frame = CGRectMake(0, 0, 64, 64);
    closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    UIBarButtonItem *closeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeButton];
    self.navigationItem.leftBarButtonItem = closeButtonItem;
    
    UIButton *clearButton = [UIButton new];
    [clearButton addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside];
    [clearButton setTitle:@"清除" forState:UIControlStateNormal];
    [clearButton setTitleColor:[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    clearButton.titleLabel.font = [UIFont systemFontOfSize:13];
    clearButton.frame = CGRectMake(0, 0, 64, 64);
    clearButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    UIBarButtonItem *clearButtonItem = [[UIBarButtonItem alloc] initWithCustomView: clearButton];
    self.navigationItem.rightBarButtonItem = clearButtonItem;
}

- (void)configUI
{
    [self configNavigationBar];
    
//    UIBarButtonItem *clearButton = [UIBarButtonItem alloc]

    // add views
    [self.view addSubview:self.calendarView];
    self.calendarView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.calendarView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.calendarView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.calendarView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.calendarView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.view addConstraints: @[left, top, right, bottom]];
    
    [self.view addSubview:self.editView];
    self.editView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *editLeft = [NSLayoutConstraint constraintWithItem:self.editView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *editRight = [NSLayoutConstraint constraintWithItem:self.editView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    NSLayoutConstraint *editBottom = [NSLayoutConstraint constraintWithItem:self.editView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    
    NSLayoutConstraint *editHeight = [NSLayoutConstraint constraintWithItem:self.editView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:180];
    [self.editView addConstraint:editHeight];
    [self.view addConstraints:@[editLeft, editRight, editBottom]];
}

- (void)clearAction
{
    [self.calendarView deSelectedAllDatesAnimated:YES];
    [self.editView resetDateSource];
}

- (void)closeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.calendarView scrollToTodayWithScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}

// MARK: - synthetize
- (JXCalendarView *)calendarView
{
    if (_calendarView == nil) {
        _calendarView = [JXCalendarView new];
        _calendarView.delegate = self;
//        _calendarView.startYear = _calendarView.endYear = 2019;
        
    }
    return _calendarView;
}

- (JXSelectedDateEditView *)editView
{
    if (_editView == nil) {
        _editView = [[JXSelectedDateEditView alloc] initWithFrame:CGRectZero];
        _editView.delegate = self;
    }
    return _editView;
}

// MARK: - calendar view delegate

- (void)jxCalendarViewDidSelectedDates:(NSArray<JXCalendarModel *> *)dates
{
    [self.editView configWithModels:dates];
}

// MARK: - edit view delegate
- (void)JXSelectedDateEditView:(JXSelectedDateEditView *)editView submitButtonTappedWithStartDate:(JXCalendarModel *)start endDate:(JXCalendarModel *)end
{
    NSLog(@"start: %d年%d月%d日， end: %d年%d月%d日",
          start.year, start.month, start.day,
          end.year, end.month, end.day);
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
